import axios from 'axios'

export default {
  data() {
    return {
      itemIndex:-1
    }
  },
  created() {
  },
  methods: {
    getValByPath(index, keys, obj) {
      if (index == keys.length - 1) {
        return obj[keys[index]];
      }
      return this.getValByPath(index+1, keys, obj[keys[index]]);
    },
    getRequest(){
      let keys = Object.keys(this.ruleForm)
      let queryStr = ''
      keys.forEach((item,index)=>{
        if(index!=keys.length-1){
          queryStr += item +'='+this.ruleForm[item] +'&'
        }else{
          queryStr += item +'='+this.ruleForm[item]
        }
      })
      axios.get(this.selectItemOptions.url+'?'+queryStr,{
          params:{},
      }).then((res)=>{
        let arr = this.selectItemOptions.resultPath.split('.')
        let valStr = this.getValByPath(0, arr, res)
        this.$set(this.divForm,this.selectItemOptions.filedName,valStr)
        this.dialogVisible = false
      }).catch((error)=>{
        this.$set(this.divForm,this.selectItemOptions.filedName,this.selectItemOptions.defaultValue)
      })
    },
    postRequest(){
      axios.post(
        this.selectItemOptions.url,
        this.ruleForm,
       {
        headers:{
        'Accept': '*/*'        }
       }
        ).then((res)=>{
        let arr = this.selectItemOptions.resultPath.split('.')
        let valStr = this.getValByPath(0, arr, res)
        this.$set(this.divForm,this.selectItemOptions.filedName,valStr)
        this.dialogVisible = false
      }).catch((error)=>{
        this.$set(this.divForm,this.selectItemOptions.filedName,this.selectItemOptions.defaultValue)
      })
    },
    requestTrendsfun(){
      this.valStr = ""
      if(this.selectItemOptions.requestMethod && this.selectItemOptions.requestMethod.toLowerCase()=='get'){
        this.getRequest()
      }
      if(this.selectItemOptions.requestMethod && this.selectItemOptions.requestMethod.toLowerCase()=='post'){
        this.postRequest()
      }
    }
  }
}
